package queryfish.antlr.parser;

import java.util.ArrayList;
import java.util.StringTokenizer;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;

public class ParserQues {
	public String question = "";
	public Boolean test = false;
	public Boolean questionWh = false;
	public Boolean s = false;
	public int count= 0;
	public String methodParser(String input) 
	{
//      ArrayList<String> listToken = new ArrayList<String>();
		ANTLRInputStream output = new ANTLRInputStream(input);
	    quesLexer lexer = new quesLexer(output);
	    CommonTokenStream tokens = new CommonTokenStream(lexer);
	    quesParser parser = new quesParser(tokens);
	    parser.setBuildParseTree(true);
	    ParserRuleContext tree = parser.question();
	    //tree.inspect(parser);
	    question = tree.toStringTree(parser);
	    return question;
	}
	 public void quest (String ss, String sq)
	    {
		 	s = false;
		 	count = 0;
	        int st = 0;
	        for (int a = 0; a < sq.length(); a++)
	        {
	            if (sq.charAt(a) == ss.charAt(st))
	            {
	                if (st == (ss.length()-1)) 
	                {
	                   s = true;
	                   count ++;
	                    
	                }
	                else st++;
	                
	            }
	            else
	            {   
	                st = 0;
	            }         
	        }
	        
	       
	    }
	 public void testANTRL (String input)
	    {
		 	quest ("missing", question);
		 	test = s;
	        quest("questionWh", question );
	        questionWh = s;
	        if (questionWh == false) test = true;
	        if (test == false)
	        {
	            StringTokenizer st1 = new StringTokenizer(input, " ?");
	            StringTokenizer st2 = new StringTokenizer(question, " )(?");
	            ArrayList<String> s1 = new ArrayList<>();
	            ArrayList<String>s2 = new ArrayList<>();
	            while (st1.hasMoreTokens()) s1.add(st1.nextToken());
	            while (st2.hasMoreTokens()) s2.add(st2.nextToken());
	            for (int i = 0; i<s1.size(); i++)
	            {
	                for(int j = 0; j<s2.size(); j++)
	                {
	                    if (s1.get(i).equals(s2.get(j)))
	                    {
	                        break;
	                    }
	                    if ((j == s2.size()-1) && (!s1.get(i).equals(s2.get(j))))
	                    {
	                        test = true;
	                    }
	                }
	            }
	        }
	    }
}
